home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / app / tips_dialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-07  |  9.6 KB  |  339 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <string.h>
  25.  
  26. #include <gtk/gtk.h>
  27.  
  28. #include "tips_dialog.h"
  29. #include "gimprc.h"
  30. #include "gimpui.h"
  31.  
  32. #include "libgimp/gimpenv.h"
  33.  
  34. #include "libgimp/gimpintl.h"
  35.  
  36. #include "wilber.h"
  37.  
  38.  
  39. #define TIPS_DIR_NAME   "tips"
  40.  
  41. static void   tips_dialog_destroy (GtkWidget *widget, gpointer data);
  42. static void   tips_show_previous  (GtkWidget *widget, gpointer data);
  43. static void   tips_show_next      (GtkWidget *widget, gpointer data);
  44. static void   tips_toggle_update  (GtkWidget *widget, gpointer data);
  45. static void   read_tips_file      (gchar *filename);
  46.  
  47. static GtkWidget  *tips_dialog = NULL;
  48. static GtkWidget  *tips_label;
  49. static gchar     **tips_text = NULL;
  50. static gint        tips_count = 0;
  51. static gint        old_show_tips;
  52.  
  53. void
  54. tips_dialog_create (void)
  55. {
  56.   GtkWidget *vbox;
  57.   GtkWidget *vbox2;
  58.   GtkWidget *hbox;
  59.   GtkWidget *bbox;
  60.   GtkWidget *frame;
  61.   GtkWidget *preview;
  62.   GtkWidget *button;
  63.   gchar     *temp;
  64.   guchar    *utemp;
  65.   guchar    *src;
  66.   guchar    *dest;
  67.   gint       x;
  68.   gint       y;
  69.  
  70.   if (tips_count == 0)
  71.     {
  72.       temp = g_strdup_printf ("%s" G_DIR_SEPARATOR_S TIPS_DIR_NAME
  73.                               G_DIR_SEPARATOR_S "%s",
  74.                   gimp_data_directory (),
  75.       /*  The string "gimp_tips.txt" needs special handling by
  76.           translators. It is used to identify the tips file to
  77.           be used for the current locale. Please translate it
  78.           accordingly. For example for the german locale that
  79.           would be "gimp_tips.de.txt". */
  80.                               _("gimp_tips.txt"));
  81.  
  82.       read_tips_file (temp);
  83.       g_free (temp);
  84.     }
  85.  
  86.   if (last_tip >= tips_count || last_tip < 0)
  87.     last_tip = 0;
  88.  
  89.   if (!tips_dialog)
  90.     {
  91.       tips_dialog = gtk_window_new (GTK_WINDOW_DIALOG);
  92.       gtk_window_set_wmclass (GTK_WINDOW (tips_dialog),
  93.                               "tip_of_the_day", "Gimp");
  94.       gtk_window_set_title (GTK_WINDOW (tips_dialog),
  95.                             _("GIMP Tip of the Day"));
  96.       gtk_window_set_position (GTK_WINDOW (tips_dialog), GTK_WIN_POS_CENTER);
  97.       gtk_window_set_policy (GTK_WINDOW (tips_dialog), FALSE, TRUE, FALSE);
  98.  
  99.       gtk_signal_connect (GTK_OBJECT (tips_dialog), "delete_event",
  100.               GTK_SIGNAL_FUNC (tips_dialog_destroy),
  101.               NULL);
  102.       gtk_signal_connect (GTK_OBJECT (tips_dialog), "destroy",
  103.               GTK_SIGNAL_FUNC (gtk_widget_destroyed),
  104.               &tips_dialog);
  105.  
  106.       /* destroy the tips window if the mainlevel gtk_main() function is left */
  107.       gtk_quit_add_destroy (1, GTK_OBJECT (tips_dialog));
  108.  
  109.       vbox = gtk_vbox_new (FALSE, 0);
  110.       gtk_container_add (GTK_CONTAINER (tips_dialog), vbox);      
  111.       gtk_widget_show (vbox);
  112.  
  113.       hbox = gtk_hbox_new (FALSE, 5);
  114.       gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
  115.       gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  116.       gtk_widget_show (hbox);
  117.  
  118.       tips_label = gtk_label_new (tips_text[last_tip]);
  119.       gtk_label_set_justify (GTK_LABEL (tips_label), GTK_JUSTIFY_LEFT);
  120.       gtk_misc_set_alignment (GTK_MISC (tips_label), 0.5, 0.5);
  121.       gtk_box_pack_start (GTK_BOX (hbox), tips_label, TRUE, TRUE, 3);
  122.       gtk_widget_show (tips_label);
  123.      
  124.       vbox2 = gtk_vbox_new (FALSE, 0);
  125.       gtk_box_pack_end (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
  126.       frame = gtk_frame_new (NULL);
  127.       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  128.       gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, FALSE, 0);
  129.  
  130.       preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  131.       gtk_preview_size (GTK_PREVIEW (preview), wilber_width, wilber_height);
  132.       utemp = g_new (guchar, wilber_width * 3);
  133.       src = (guchar *)wilber_data;
  134.       for (y = 0; y < wilber_height; y++)
  135.     {
  136.       dest = utemp;
  137.       for (x = 0; x < wilber_width; x++)
  138.         {
  139.           HEADER_PIXEL (src, dest);
  140.           dest += 3;
  141.         }
  142.       gtk_preview_draw_row (GTK_PREVIEW (preview), utemp, 0, y, wilber_width); 
  143.     }
  144.       g_free(utemp);
  145.       gtk_container_add (GTK_CONTAINER (frame), preview);
  146.       gtk_widget_show (preview);
  147.       gtk_widget_show (frame);
  148.       gtk_widget_show (vbox2);
  149.  
  150.       hbox = gtk_hbox_new (FALSE, 15);
  151.       gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
  152.       gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  153.       gtk_widget_show (hbox);
  154.       
  155.       button = 
  156.     gtk_check_button_new_with_label (_("Show tip next time GIMP starts"));
  157.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
  158.                     show_tips);
  159.       gtk_signal_connect (GTK_OBJECT (button), "toggled",
  160.               GTK_SIGNAL_FUNC (tips_toggle_update),
  161.               (gpointer) &show_tips);
  162.       gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  163.       gtk_widget_show (button);
  164.  
  165.       old_show_tips = show_tips;
  166.  
  167.       bbox = gtk_hbutton_box_new ();
  168.       gtk_box_pack_end (GTK_BOX (hbox), bbox, FALSE, FALSE, 0);
  169.       gtk_widget_show (bbox);
  170.  
  171.       button = gtk_button_new_with_label (_("Close"));
  172.       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  173.       gtk_window_set_default (GTK_WINDOW (tips_dialog), button);
  174.       gtk_signal_connect (GTK_OBJECT (button), "clicked",
  175.               GTK_SIGNAL_FUNC (tips_dialog_destroy),
  176.               NULL);
  177.       gtk_container_add (GTK_CONTAINER (bbox), button);
  178.       gtk_widget_show (button);
  179.  
  180.       bbox = gtk_hbutton_box_new ();
  181.       gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
  182.       gtk_button_box_set_spacing (GTK_BUTTON_BOX (bbox), 5);
  183.       gtk_box_pack_end (GTK_BOX (hbox), bbox, FALSE, FALSE, 0);
  184.       gtk_widget_show (bbox);
  185.  
  186.       button = gtk_button_new_with_label (_("Previous Tip"));
  187.       GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
  188.       gtk_signal_connect (GTK_OBJECT (button), "clicked",
  189.               GTK_SIGNAL_FUNC (tips_show_previous),
  190.               NULL);
  191.       gtk_container_add (GTK_CONTAINER (bbox), button);
  192.       gtk_widget_set_sensitive (button, (tips_count > 1));
  193.       gtk_widget_show (button);
  194.  
  195.       button = gtk_button_new_with_label (_("Next Tip"));
  196.       GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
  197.       gtk_signal_connect (GTK_OBJECT (button), "clicked",
  198.               GTK_SIGNAL_FUNC (tips_show_next),
  199.               NULL);
  200.       gtk_container_add (GTK_CONTAINER (bbox), button);
  201.       gtk_widget_set_sensitive (button, (tips_count > 1));
  202.       gtk_widget_show (button);
  203.  
  204.      /*  Connect the "F1" help key  */
  205.       gimp_help_connect_help_accel (tips_dialog,
  206.                     gimp_standard_help_func,
  207.                     "dialogs/tip_of_the_day.html");
  208.     }
  209.  
  210.   if (!GTK_WIDGET_VISIBLE (tips_dialog))
  211.     {
  212.       gtk_widget_show (tips_dialog);
  213.     }
  214.   else
  215.     {
  216.       gdk_window_raise (tips_dialog->window);
  217.     }
  218. }
  219.  
  220. static void
  221. tips_dialog_destroy (GtkWidget *widget,
  222.              gpointer   data)
  223. {
  224.   GList *update = NULL; /* options that should be updated in .gimprc */
  225.   GList *remove = NULL; /* options that should be commented out */
  226.  
  227.   gtk_widget_destroy (tips_dialog);
  228.  
  229.   /* the last-shown-tip is now saved in sessionrc */
  230.  
  231.   if (show_tips != old_show_tips)
  232.     {
  233.       update = g_list_append (update, "show-tips");
  234.       remove = g_list_append (remove, "dont-show-tips");
  235.       old_show_tips = show_tips;
  236.       save_gimprc (&update, &remove);
  237.     }
  238.   g_list_free (update);
  239.   g_list_free (remove);
  240. }
  241.  
  242. static void
  243. tips_show_previous (GtkWidget *widget,
  244.             gpointer  data)
  245. {
  246.   last_tip--;
  247.  
  248.   if (last_tip < 0)
  249.     last_tip = tips_count - 1;
  250.  
  251.   gtk_label_set_text (GTK_LABEL (tips_label), tips_text[last_tip]);
  252. }
  253.  
  254. static void
  255. tips_show_next (GtkWidget *widget,
  256.         gpointer   data)
  257. {
  258.   last_tip++;
  259.  
  260.   if (last_tip >= tips_count)
  261.     last_tip = 0;
  262.  
  263.   gtk_label_set_text (GTK_LABEL (tips_label), tips_text[last_tip]);
  264. }
  265.  
  266. static void
  267. tips_toggle_update (GtkWidget *widget,
  268.             gpointer   data)
  269. {
  270.   gint *toggle_val;
  271.  
  272.   toggle_val = (gint *) data;
  273.  
  274.   if (GTK_TOGGLE_BUTTON (widget)->active)
  275.     *toggle_val = TRUE;
  276.   else
  277.     *toggle_val = FALSE;
  278. }
  279.  
  280. static void
  281. store_tip (gchar *str)
  282. {
  283.   tips_count++;
  284.   tips_text = g_realloc (tips_text, sizeof (gchar *) * tips_count);
  285.   tips_text[tips_count - 1] = str;
  286. }
  287.  
  288. static void
  289. read_tips_file (gchar *filename)
  290. {
  291.   FILE *fp;
  292.   gchar *tip = NULL;
  293.   gchar *str = NULL;
  294.  
  295.   fp = fopen (filename, "rt");
  296.   if (!fp)
  297.     {
  298.       store_tip (_("Your GIMP tips file appears to be missing!\n"
  299.            "There should be a file called gimp_tips.txt in\n"
  300.            "the tips subdirectory of the GIMP data directory.\n"
  301.            "Please check your installation."));
  302.       return;
  303.     }
  304.  
  305.   str = g_new (char, 1024);
  306.   while (!feof (fp))
  307.     {
  308.       if (!fgets (str, 1024, fp))
  309.     continue;
  310.    
  311.       if (str[0] == '#' || str[0] == '\n')
  312.     {
  313.       if (tip != NULL)
  314.         {
  315.           tip[strlen (tip) - 1] = '\000';
  316.           store_tip (tip);
  317.           tip = NULL;
  318.         }
  319.     }
  320.       else
  321.     {
  322.       if (tip == NULL)
  323.         {
  324.           tip = g_malloc (strlen (str) + 1);
  325.           strcpy (tip, str);
  326.         }
  327.       else
  328.         {
  329.           tip = g_realloc (tip, strlen (tip) + strlen (str) + 1);
  330.           strcat (tip, str);
  331.         }
  332.     }
  333.     }
  334.   if (tip != NULL)
  335.     store_tip (tip);
  336.   g_free (str);
  337.   fclose (fp);
  338. }
  339.